##=============================================================================
## Appendix Figure 6
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "hrbrthemes")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##--------------------------
# VIINA by combat event type
viina %>% 
  dplyr::select(yrmo, t_aad_b:t_hospital_b) %>% 
  filter(yrmo < "2023-04-01") %>% 
  mutate(wave = ifelse(yrmo > "2022-07-01", "Wave II", "Wave I")) %>% 
  mutate(wave = factor(wave, levels = c("Wave I", "Wave II"))) %>% 
  dplyr::select(wave, t_aad_b:t_hospital_b) %>% 
  pivot_longer(cols = !wave, names_to = "attack_type", 
               values_to = "count") %>% 
  filter(count == 1) %>% 
  dplyr::select(-count) %>% 
  mutate(attack_type = case_when(
    attack_type == "t_aad_b" ~ "Anti-air defense",
    attack_type == "t_airstrike_b" ~ "Air or helicopter strike",
    attack_type == "t_armor_b" ~ "Tank battle or assault",
    attack_type == "t_arrest_b" ~ "Arrest or detention \n by security services",
    attack_type == "t_artillery_b" ~ "Artillery shelling",
    attack_type == "t_control_b" ~ "Capture of building \n or territory",
    attack_type == "t_firefight_b" ~ "Gunfire exchange",
    attack_type == "t_hospital_b" ~ "Attacks on hospitals \n and humanitarian convoys",
    attack_type == "t_ied_b" ~ "Improvised explosive device \n or roadside bomb",
    attack_type == "t_occupy_b" ~ "Occupation of territory \n or building",
    attack_type == "t_property_b" ~ "Destruction of property \n or infrastructure",
    attack_type == "t_raid_b" ~ "Assault/attack by paratroopers \n or special forces",
    attack_type == "t_cyber_b" ~ "Cyber attack on civilian or \n military infrastructure")) %>% 
  group_by(wave) %>% 
  count(attack_type) %>% 
  ggplot(., aes(x = reorder(attack_type, n), n, fill = wave)) + 
  geom_bar(position = "dodge", stat = "identity") +
  coord_flip() +
  scale_fill_grey() +
  labs(title = "Combat events by type", 
       fill = "Survey Wave",
       x = NULL, 
       y = "Event Count",
       caption = "Source: VIINA (Zhukov 2023)") +
  hrbrthemes::theme_ipsum(axis_title_size = 16, 
                          axis_text_size = 14,
                          plot_title_size = 24,
                          caption_size = 16,
                          base_size = 12) +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        legend.title = element_blank(),
        legend.text = element_text(size = 16))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================